package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController//= controller+@ResponseBody
@RequestMapping("/department")
/**
 * 一些新的注解
 *     @GetMapping=@RequestMapping(method = RequestMethod.GET)
 @PostMapping
 @DeleteMapping
 @PatchMapping
 @PutMapping
 */
@Api(value = "/department",description = "组织结构模块部门管理的接口规范！")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;
    //添加
    //修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId()==null){
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败！"+e.getMessage());
        }
    }

    //删除
    @DeleteMapping("/{id}")
    //?????返回值是什么，这个方法一般是Ajax调用的，我们应该对它进行响应
    //1格式：一般会给一个成功或者失败+提示信息，并且这个信息会以Json的方式返回，格式如
    //{"success":true,"message":"操作成功“} or "success":false,"message":"删除失败，nullpointexception“}
    //2 怎么返回
    //2.1 方案拼接字符串 傻逼公司
    //2.2 Map<String,String> sucess-true,message:xxx 屌丝公司 @ResponseBody或@RestController,最终会把Map转换为json字符串
    //2.3 封装一个对象，抽取一个类AjaxResult,里面以后两个属性success和message  @ResponseBody或@RestController,最终会把AjaxResult转换为json字符串
    @ApiOperation(value = "部门删除",notes = "通过id删除部门")
    public AjaxResult del(@PathVariable("id") @ApiParam("唯一表示id") Long id){

        //方案1： 两个构造函数
        //        try {
        //            departmentService.delete(id);
        //            return new AjaxResult();
        //        } catch (Exception e) {
        //            e.printStackTrace();
        //            return new AjaxResult("删除失败！"+e.getMessage());
        //        }

//        //方案2：静态方法
//                try {
//                    departmentService.delete(id);
//                    return AjaxResult.me();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    return new AjaxResult("删除失败！"+e.getMessage());
//                }
        //方案3：静态方法+链式编程
                try {
                    departmentService.delete(id);
                    return AjaxResult.me();
                } catch (Exception e) {
                    e.printStackTrace();
                    return  AjaxResult.me().setSuccess(false).setMessage("删除失败！"+e.getMessage());
                }
    }


    //查询
    //1 查询一个 GET  /department/1/xxx
    // @GetMapping("/{id}/{xxx}")
    // public Department getById(@PathVariable(value = "id")Long id,@PathVariable(value = "xxx")String xxx);
    @GetMapping("/{id}") //  get /department/1
    public Department getById(@PathVariable(value = "id")Long id){
       return departmentService.getById(id);
    }
    //2 查询所有
    @GetMapping// get /department
    public List<Department> getAll(){
        return departmentService.getAll();
    }
    //3 高级查询+分页 Post
    // 1 参数（Query）=查询条件+分页条件（当前页+每页显示多少条）
    //2 返回值=总数+当前页数据
    @PostMapping  //前台是通过Json对象传递进来的，而且是放到RequestBody
    public PageList<Department> list(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }
}
